//
//  CRSetHeaderView.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/25.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//
#import "CRSetHeaderView.h"
#import "UIImageView+WebCache.h"
#import "CRUserDetailModel.h"
@interface CRSetHeaderView ()

@property (nonatomic, strong) UIImageView *iconImageView; // 头像
@property (nonatomic, strong) UILabel *nameLabel; // 名字
@property (nonatomic, strong) UILabel *signatureLabel; // 个性签名

@end


@implementation CRSetHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}


- (void)setupSubviews {
    // 设置头像
    self.iconImageView = [[UIImageView alloc] init];
    self.iconImageView.userInteractionEnabled = YES;
    
    // 添加轻拍手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapGesture:)];
    [self.iconImageView addGestureRecognizer:tapGesture];
    [self addSubview:self.iconImageView];
    
    // 设置名字
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = kDefaultFont;
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nameLabel];
    
    // 设置个性签名
    self.signatureLabel = [[UILabel alloc] init];
    self.signatureLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.signatureLabel.textColor = [UIColor whiteColor];
    self.signatureLabel.font = kDefaultSignatureFont;
    self.signatureLabel.numberOfLines = 0;
    [self addSubview:self.signatureLabel];
}

- (void)setUserDetailM:(CRUserDetailModel *)userDetailM {
    
    _userDetailM = userDetailM;


    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:userDetailM.iconUrl] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.nameLabel.text = userDetailM.username;
    self.signatureLabel.text = userDetailM.signature;
    
    CGFloat iconX = kDefaultHeaderMargin;
    CGFloat iconY = iconX;
    CGFloat iconW = 100;
    CGFloat iconH = iconW;
    self.iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    self.iconImageView.layer.cornerRadius = self.iconImageView.width * 0.5;
    self.iconImageView.clipsToBounds = YES;
    self.iconImageView.layer.borderWidth = 1;
    self.iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    CGFloat nameX = iconW + 2 * kDefaultHeaderMargin;
    CGFloat nameY = kDefaultHeaderMargin * 2;
    CGSize nameSize = [userDetailM.username sizeWithFont:kDefaultFont];
    self.nameLabel.frame = (CGRect){{nameX, nameY}, nameSize};
    
    CGFloat signatureX = nameX;
    CGFloat signatureY = CGRectGetMaxY(self.nameLabel.frame) + kDefaultHeaderMargin;
    CGSize signatureSize = [userDetailM.signature sizeWithFont:kDefaultSignatureFont maxW:self.width - signatureX];
    self.signatureLabel.frame = (CGRect){{signatureX, signatureY}, signatureSize};
    
}

#pragma mark >>>> 监听事件
- (void)didTapGesture:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"点击了头像");
    if ([self.delegate respondsToSelector:@selector(didIconImageViewClick)]) {
        [self.delegate didIconImageViewClick];
    }
}

@end
